package com.china08.yxyapi.api.manage.edu.platform.continuedu.renwu;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduRenWuUpdController extends YxyApiBaseController {

    @Autowired
    YxyContinueduTaskRepository taskRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/renwu/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws ParseException {

        String taskId = reqModel.taskId;
        String taskName = reqModel.taskName;
        Date beginDate = reqModel.beginDate;
        Date endDate = reqModel.endDate;
        Integer taskScore = reqModel.taskScore;
        YxyContinueduTask task = taskRepository.findById(taskId);
        if (null != task) {
            task.setName(taskName);
            task.setBeginDate(beginDate);
            task.setEndDate(endDate);
            task.setScore(taskScore);

            // 1513094400000
            // 1513008000000
            // 1513094400000
            // 判断时间
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String date = formatter.format(System.currentTimeMillis());
            Date date2 = formatter.parse(date);
            long l1 = date2.getTime();
            long l2 = beginDate.getTime();
            long l3 = endDate.getTime();

            // 系统时间==结束时间：进行中
            if (l1 == l3) {
                task.setStatus(0);
            }
            // 当前系统时间==开始时间：进行中
            else if (l1 == l2) {
                task.setStatus(0);
            }
            // 当前系统时间<开始时间: 未开始
            else if (l1 < l2) {
                task.setStatus(-1);
            }
            // 当前系统时间>开始时间，并且小于结束时间：进行中
            else if (l1 > l2 && l1 < l3) {
                task.setStatus(0);
            }
            // 当前系统时间>开始时间，并且大于结束时间：已截止
            else if (l1 > l2 && l1 > l3) {
                task.setStatus(1);
            }
            taskRepository.save(task);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String taskId;
        public String taskName;
        public Integer taskScore;
        public Date beginDate;
        public Date endDate;
    }

}
